// gruntfile.js
module.exports = grunt => {

    let port = 2080;
    let branch = 'gh-pages';
    for(const args of process.argv){
        if(args.startsWith('--port=')) {
            const p = args.slice(7)
            if(!isNaN(Number(p))){
                port = Number(p)
            }
        }
        if(args.startsWith('--branch=')) {
            branch = args.slice(9)            
        }
    }

    require('load-grunt-tasks')(grunt)

    grunt.initConfig({
        clean: {
            temp: 'temp',
            dist: 'dist'
        },
        sass: {
            temp: {
                files: [{
                    expand: true,
                    cwd: './src/assets/styles',
                    src: ['*.scss'],
                    dest: 'temp/assets/styles',
                    ext: '.css'
                }]
            }
        },
        babel: {
            options: {
              sourceMap: true,
              presets: ['@babel/preset-env']
            },
            temp: {
                files: [{
                    expand: true,
                    cwd: './src/assets/scripts',
                    src: ['*.js'], 
                    dest: 'temp/assets/scripts'
                }]
            }
        },
        swigtemplates: {
            options: {
                defaultContext: require('./data'),
                templatesDir: 'src',
            },     
            staging: {              
              dest: 'temp/',
              src: ['src/*.html']
            }
        },
        imagemin: {
            build: {
                expand: true,
                cwd: 'src/assets/images',
                src: '**',
                dest: 'dist/assets/images'
            },
            buildFont: {
                expand: true,
                cwd: 'src/assets/fonts',
                src: '**',
                dest: 'dist/assets/fonts'
            },
        },
        htmlmin: {
            file: {
                options: {
                    removeComments: true,
                    collapseWhitespace: true
                },
                files: [{
                    expand: true,
                    cwd: 'temp',
                    src: '*.html',
                    dest: 'dist'
                }]
            }
        },
        copy: {
            public: {
                expand: true,
                cwd: 'public',
                src: '**',
                dest: 'dist'
            },
            temp: {
                expand: true,
                cwd: 'temp',
                src: '**',
                dest: 'dist'
            },
            concat: {
                expand: true,
                cwd: '.tmp/concat',
                src: '**',
                dest: 'dist'
            },
            image: {
                expand: true,
                cwd: 'src/assets/images',
                src: '**',
                dest: 'dist/assets/images'
            },
            font: {
                expand: true,
                cwd: 'src/assets/fonts',
                src: '**',
                dest: 'dist/assets/fonts'
            }
        },
        useminPrepare: {
            html: 'temp/*.html',
            options: {
                dest: 'dist',
                root: ['temp', '.']
            }
        },
        usemin: {
            html: 'temp/*.html',
        },        
        browserSync: {
            options: {
                open: process.argv.includes('--open'),
                notify: false,
                port
            },
            serve: {
                bsFiles: {
                    src: ['temp', 'src', 'public']
                },
                options: {
                    watchTask: true,
                    server: {
                        baseDir: ['temp', 'src', 'public'],
                        routes: {
                            "/node_modules": "node_modules"
                        }
                    }
                }
            },
            start: {
                bsFiles: {
                    src: ['dist']
                },
                options: {
                    watchTask: true,
                    server: {
                        baseDir: ['dist'],                        
                    }
                }
            }
        },
        watch: {
            sass: {
                files: 'src/assets/styles/*.scss',
                tasks: ['sass']
            },
            js: {
                files: 'src/assets/scripts/*.js',
                tasks: ['babel']
            }
        },
        eslint: {
            target: ['src/assets/scripts/*.js']
        },
        sasslint: {
            target: [
              'src/assets/styles/*.scss',
            ],
        },
        exec: {
            pull: {
                cmd: 'git pull'
            },            
            checkout: {
                cmd: `git checkout ${branch}`
            },
            add: {
                cmd: 'git add dist'
            },
            commit: {
                cmd: 'git commit -m "deploy dist"'
            },
            push: {
                cmd: 'git push'
            },
        }
    })

    grunt.registerTask('compile', ['sass', 'babel', 'swigtemplates'])

    if(process.argv.includes('--production') || process.argv.includes('--prod')) {
        grunt.registerTask('build', [
            'clean',
            'compile', 
            'useminPrepare', 
            'concat',
            'cssmin',
            'uglify',
            'usemin',
            // 'imagemin',
            'htmlmin',
            'copy:public'
        ])
    } else {
        grunt.registerTask('build', [
            'clean',
            'compile', 
            'useminPrepare', 
            'concat',
            'usemin',
            'copy'
        ])
    }

    grunt.registerTask('serve', ['compile', 'browserSync:serve', 'watch'])

    grunt.registerTask('start', ['build', 'browserSync:start', 'watch'])

    grunt.registerTask('lint', ['eslint', 'sasslint'])

    grunt.registerTask('deploy', ['pull', 'checkout', 'add', 'commit', 'push'])
}